package com.lzj.server.utils;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.lzj.server.constant.enums.ResultCodeEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 通用返回结果封装
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> implements Serializable {

    private Integer code;
    private String msg;
    private T data;

    // ==================== 成功 ====================

    public static <T> Result<T> success() {
        return new Result<>(ResultCodeEnum.SUCCESS.code(), ResultCodeEnum.SUCCESS.message(), null);
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(ResultCodeEnum.SUCCESS.code(), ResultCodeEnum.SUCCESS.message(), data);
    }

    public static <T> Result<T> success(String msg, T data) {
        return new Result<>(ResultCodeEnum.SUCCESS.code(), msg, data);
    }

    // ==================== 失败 ====================

    public static <T> Result<T> error(String msg) {
        return new Result<>(ResultCodeEnum.FAIL.code(), msg, null);
    }

    public static <T> Result<T> error(ResultCodeEnum resultCode) {
        return new Result<>(resultCode.code(), resultCode.message(), null);
    }

    public static <T> Result<T> error(int code, String msg) {
        return new Result<>(code, msg, null);
    }

    // ==================== 辅助方法 ====================
    @JsonIgnore // 不参与序列化
    @Schema(hidden = true) // 不参与文档生成
    public boolean isSuccess() {
        return this.code != null && this.code.equals(ResultCodeEnum.SUCCESS.code());
    }

    @JsonIgnore // 不参与序列化
    @Schema(hidden = true) // 不参与文档生成
    public boolean isFail() {
        return !isSuccess();
    }
}
